/*
 *  main.c
 *
 *  $Id: main.c 22 2009-08-22 14:02:46Z nramsbottom $	
 *
 */

#include <stdio.h>

#include "game.h"
#include "display.h"
#include "log.h"
#include "language.h"

const char* debug_log = NULL;
const char* language_switch = NULL;

#define DEFAULT_LANGUAGE	"no"

int main (int argc, const char** argv) {

	const char** arg = argv;
	
	if (argc > 1) {
		do {
			if ((*arg)[0] == '-') {
				switch ((*arg)[1]) {
				
					case 'd':
						debug_log = *(++arg); // NGR: I'm not happy with this. This could point to anything!
						break;
						
					case 'l':
						language_switch = *(++arg);
						break;
						
					default:
						// ignore
						break;
				}
			}
		} while (*(++arg) != NULL);
	}
	
	if (debug_log != NULL) {
		if (!log_open(debug_log))
			printf("Warning: Unable to open debug output file.\n");
	}
	
	int languageid = LANG_DEFAULT;
	
	if (language_switch) {
		languageid = language_findidbycode(language_switch);
		if (!languageid) {
			fprintf(stderr, "Warning: Unable to locate localised strings for %s. Using default.\n", language_switch);
			languageid = LANG_DEFAULT;
		}
	}
	
	language_setcurrent(languageid);
	if (languageid != LANG_DEFAULT)
		printf("Using %s\n", language_getname(languageid));
	
	display_startup();
	game_run();
	display_shutdown();
	
	// close/cleanup logging 
	log_close();
	
	return 0;
}
